<!doctype html>
<html lang="en">
	<head>
		<title>Tween.js / hello world!</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
	</head>
	<body>
		<div id="info">
			<h1><a href="http://github.com/tweenjs/tween.js">tween.js</a></h1>
			<h2>00 _ hello world</h2>
			<p>Simple example for illustrating the creation and chaining of tweens.</p>
		</div>
		<div
			id="target"
			style="
				position: absolute;
				transform: translate(100px, 100px);
				width: 100px;
				height: 100px;
				background: #a0dde9;
				padding: 1em;
			"
		>
			hello world!
		</div>

		<script type="module">
			import {Tween, Easing, Group} from '../dist/tween.esm.js'

			const position = {x: 100, y: 100, rotation: 10}
			const target = document.getElementById('target')
			const tween = new Tween(position)
				.to({x: 700, y: 200, rotation: 359}, 2000)
				.delay(1000)
				.easing(Easing.Elastic.InOut)
				.onUpdate(update)

			const tweenBack = new Tween(position)
				.to({x: 100, y: 100, rotation: 10}, 3000)
				.easing(Easing.Elastic.InOut)
				.onUpdate(update)

			tween.chain(tweenBack)
			// tweenBack.chain(tween)

			tween.start()

			const group = new Group(tween, tweenBack)

			animate(performance.now())

			function animate(time) {
				group.update(time)

				// If the update method returns false, it means all tweens in
				// the group are done playing, so we can stop the loop.
				const keepGoing = !group.allStopped()

				if (keepGoing) requestAnimationFrame(animate)
			}

			function update() {
				target.style.transform = `translate3d(${position.x}px, ${position.y}px, 0.0001px) rotateY(${Math.floor(
					position.rotation,
				)}deg)`
			}
		</script>
	</body>
</html>
